package com.hilaver.dzmis.web.servlet.quotation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.quotation.QuYarn;
import com.hilaver.dzmis.service.impl.QuYarnServiceImpl;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class QuYarnServlet extends AbstractBaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private QuYarnServiceImpl quYarnService;
	private QuYarn quYarn;

	public QuYarnServlet() {
		super();
		this.fullName = QuYarn.class.getName();
		this.simpleName = QuYarn.class.getSimpleName();
		this.quYarnService = new QuYarnServiceImpl();
		this.quYarnService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}
	
	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		this.quYarnService.setLocale((Locale) request.getSession()
				.getAttribute(Constants.SESSION_LOCALE));
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			rtn = this.quYarnService.edit(this.quYarn);
			break;
		case ACTION_DELETE: // "delete";
			super.setId(request);
			rtn = this.quYarnService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			super.setId(request);
			rtn = this.quYarnService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION: // "get one with description"
			
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = this.quYarnService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL:
			
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.quYarn = (QuYarn)this.obj;
		
		Integer yarnId = Integer.parseInt((String)this.getValue(request, "yarnId"));
		BiYarn biYarn = new BiYarn();
		biYarn.setId(yarnId);
		this.quYarn.setBiYarn(biYarn);
	}
}
